package com.jeeplus.modules.tms.web;


import com.google.gson.Gson;
import com.jeeplus.modules.fee.util.Util;
import com.jeeplus.modules.infor.entity.Driver;
import com.jeeplus.modules.infor.entity.Goods;
import com.jeeplus.modules.infor.entity.LnCustomers;
import com.jeeplus.modules.infor.service.DriverService;
import com.jeeplus.modules.infor.service.GoodsService;
import com.jeeplus.modules.infor.service.LnCustomersService;
import com.jeeplus.modules.tms.entity.*;
import com.jeeplus.modules.tms.service.NotnodeService;
import com.jeeplus.modules.tms.service.ReceiptService;
import com.jeeplus.modules.tms.service.TailAfterService;
import com.jeeplus.modules.tms.service.TmsWaybillService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * Created by zhl on 2017/5/18.
 */
@Controller
@RequestMapping("${adminPath}/weixinx")
public class WxController {
@Autowired
private DriverService driverService;
@Autowired
private LnCustomersService lnCustomersService;
@Autowired
private TmsWaybillService tmsWaybillService;
@Autowired
private GoodsService goodsService;
@Autowired
private ReceiptService receiptService;
@Autowired
private NotnodeService notnodeService;
@Autowired
private TailAfterService tailAfterService;


    /**
     * 登录
     * @param request
     * @return
     */
    @RequestMapping("wxlogin")
    @ResponseBody
public  String  login(HttpServletRequest request){
        String userName=request.getParameter("userName") ;
        String userPassword=request.getParameter("userPassword") ;
        Driver d=new Driver();
        d.setPhone(userPassword);
        d.setName(userName);
        List<Driver> list=driverService.findList(d);
        LnCustomers l=new LnCustomers();
        l.setPerson(userName);
        l.setPhone(userPassword);
        List<LnCustomers>li=lnCustomersService.findList(l);
        if(list!=null&&list.size()>0){
            String s="{'userName':'"+userName+"','passWord':'"+userPassword+"','type':'司机'}";
            JSONObject obj=JSONObject.fromObject(s);
            return obj.toString();
        }else if(li!=null&&li.size()>0){
            String s="{'userName':'"+userName+"','passWord':'"+userPassword+"','type':'客户'}";
            JSONObject obj=JSONObject.fromObject(s);
            return obj.toString();
        }else{
            return "false";
        }


    }

    /**
     * 查找司机的单子
     */

    @ResponseBody
    @RequestMapping("getOrder")
    public String getOrder(String userName,String userPassword){
        TmsWaybill t=new TmsWaybill();
        t.setDriverPhone(userPassword);
        t.setDriverName(userName);
        t.setBillStatus("在途");
        List<TmsWaybill> l=tmsWaybillService.findList(t);
        if(l==null||l.size()<1){
            return "false";
        }
        Gson g=new Gson();
        return g.toJson(l).toString();
    }

    /**
     * 查找客户的单子
     */

    @ResponseBody
    @RequestMapping("getOrder1")
    public String getOrder1(String userName,String userPassword){
        TmsWaybill t=new TmsWaybill();
        t.setPhone(userPassword);
        t.setUnitConfirmor(userName);
        t.setBillStatus("在途");
        List<TmsWaybill> l=tmsWaybillService.findList(t);
        if(l==null||l.size()<1){
            return "false";
        }
        Gson g=new Gson();
        return g.toJson(l).toString();
    }

    /**
     * 查找客户订单装车明细
     */

    @ResponseBody
    @RequestMapping("getGoods")
    public String getGoods(String i){
        TmsWaybill t=tmsWaybillService.get(i);
        List<TmsMaterialsInformation>list =t.getTmsMaterialsInformationList();
        Gson g=new Gson();
        return g.toJson(list).toString();
    }

    /**
     * 查找客户订单装车明细
     */

    @ResponseBody
    @RequestMapping("getInfors")
    public String getInfors(String i){
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:00");
        TmsWaybill t=tmsWaybillService.get(i);
        StringBuffer sb=new StringBuffer();
        sb.append("{'fzr':'").append(t.getDispatchName().getName()).append("','dh':'").append(t.getDispatchName().getPhone()).append("',");
        sb.append("'sjdh':'").append(t.getDriverPhone()).append("','fhrq':'");
        TailAfter af=tailAfterService.findUniqueByProperty("bill",i);
        Date d=af.getThDepartureTime();
        sb.append(f.format(d)).append("','zjs':'").append(t.getZjs()).append("','zxwz':'");
        Notnode n=new Notnode();
        n.setBill(i);
        String zxwz="";
        List<Notnode> list=notnodeService.findList(n);
        if(list==null||list.size()<1){
            zxwz="仓库";
        }else{
            zxwz=list.get(0).getTrackLocation();
        }
        sb.append(zxwz).append("','yjdd':'").append(f.format(af.getYjdd())).append("'}");
        JSONObject obj=JSONObject.fromObject(sb.toString());
        return obj.toString();
    }
    /**
     * 签收订单校验
     * @param request
     * @return
     */
    @RequestMapping("search")
    @ResponseBody
    public  String  search(HttpServletRequest request){
    String code=request.getParameter("code");
     TmsWaybill t=   tmsWaybillService.findUniqueByProperty("code",code);
     if(t==null||t.getId()==null){
         return "false";
     }else if(
             t.getBillStatus().equals("已签收")){
         return "qs";
     }else{
            t=tmsWaybillService.get(t.getId());
         int a=0;
         List<TmsMaterialsInformation> l=t.getTmsMaterialsInformationList();
         for(TmsMaterialsInformation t1:l){
             int amount=Integer.parseInt(t1.getLoadingQuantity());
             Goods g=goodsService.get(t1.getName().getId());
             int bl=Integer.parseInt(g.getProportion());
             int c=amount/bl;
             a+=c;
         }
         StringBuffer sb=new StringBuffer();
         sb.append("{'id':'").append(t.getId()).append("','zy':'").append(t.getShipmentNumbers()).append("',");
         sb.append("'sl':'").append(a).append("'}");
         JSONObject obj=JSONObject.fromObject(sb.toString());
         return obj.toString();
     }
    }

    /**
     * 定位订单校验
     * @param request
     * @return
     */
    @RequestMapping("search1")
    @ResponseBody
    public  String  search1(HttpServletRequest request){
        String code=request.getParameter("code");
        TmsWaybill t=   tmsWaybillService.findUniqueByProperty("code",code);
        if(t==null||t.getId()==null){
            return "false";
        }else if(!t.getBillStatus().equals("在途")){
            return "qs";
        }else{
            t=tmsWaybillService.get(t.getId());
            int a=0;
            List<TmsMaterialsInformation> l=t.getTmsMaterialsInformationList();
            for(TmsMaterialsInformation t1:l){
                int amount=Integer.parseInt(t1.getLoadingQuantity());
                Goods g=goodsService.get(t1.getName().getId());
                int bl=Integer.parseInt(g.getProportion());
                int c=amount/bl;
                a+=c;
            }
            StringBuffer sb=new StringBuffer();
            sb.append("{'id':'").append(t.getId()).append("','zy':'").append(t.getShipmentNumbers()).append("',");
            sb.append("'sl':'").append(a).append("'}");
            JSONObject obj=JSONObject.fromObject(sb.toString());
            return obj.toString();
        }
    }


    /**
     * 签收
     * @param request
     * @return
     */
    @RequestMapping("qs")
    @ResponseBody
    public  String  qs(HttpServletRequest request){
        String id=request.getParameter("id");
        String name=request.getParameter("name");
        String sl=request.getParameter("sl");
        String zt=request.getParameter("zt");
        String qs=request.getParameter("qs");
        String qh=request.getParameter("qh");
        String pf=request.getParameter("pf");
        String bz=request.getParameter("bz");
        Receipt r=new Receipt();
        r.setBill(id);
        r.setClienteleCollectName(name);
        r.setClienteleCollectState(zt);
        r.setClienteleCompensate(pf);
        r.setClienteleEntruckingNumber(sl);
        r.setClienteleRemarks(bz);
        r.setClienteleSignNumber(qs);
        r.setClienteleSignTime(new Date());
        r.setClienteleStockout(qh);
        receiptService.save(r);
        TmsWaybill t=tmsWaybillService.get(id);
        if(zt.equals("正常")){
            t.setZt("正常");
        }else{
            t.setZt("异常");
        }
        t.setBillStatus("已签收");
        tmsWaybillService.save(t);
        return "suucess";

    }
    /**
     * 获取位置
     * @param
     * @return
     */
    @RequestMapping("location")
    @ResponseBody
    public String location(HttpServletRequest req) throws UnsupportedEncodingException {
        String latitude=req.getParameter("latitude");
        String longitude=req.getParameter("longitude");
        String url="http://apis.map.qq.com/ws/geocoder/v1/?location="+latitude+","+longitude+"&key=GGZBZ-4ZFK4-UBSUF-DJJT2-WVY7S-3RFNS";
        String result= Util.sendGet(url, "utf-8");
        return result;
    }
    /**
     * 定位
     * @param request
     * @return
     */
    @RequestMapping("dw")
    @ResponseBody
    public  String  dw(HttpServletRequest request){
        String id=request.getParameter("id");
        String name=request.getParameter("name");
        String location=request.getParameter("location");
        String bz=request.getParameter("bz");
        Notnode n=new Notnode();
        n.setBill(id);
        n.setPerson(name);
        n.setTrackType("微信跟踪");
        n.setTrackState("正常");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        n.setTrackTime(df.format(new Date()));
        n.setRemarks(bz);
        n.setTrackLocation(location);
        notnodeService.save(n);
        return "success";
    }
}

